package com.atguigu.tingshu.order.strategy;

import com.alibaba.fastjson.JSON;
import com.atguigu.tingshu.common.constant.RedisConstant;
import com.atguigu.tingshu.common.constant.SystemConstant;
import com.atguigu.tingshu.order.helper.SignHelper;
import com.atguigu.tingshu.vo.order.OrderInfoVo;
import com.atguigu.tingshu.vo.order.TradeVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;

import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

public interface OrderTradeStrategy {



    // 定义业务逻辑的抽象方法
    public abstract OrderInfoVo trade(TradeVo tradeVo) ;

    // 获取策略实现类多对应的itemType的
    public abstract String getItemType() ;

    default public void setOtherDataToOrderInfoVo(OrderInfoVo orderInfoVo ,  RedisTemplate<String , String> redisTemplate) {

        // 生成一个uuid作为交易号
        String tradeNo = UUID.randomUUID().toString().replace("-", "");
        orderInfoVo.setTradeNo(tradeNo);
        orderInfoVo.setPayWay(SystemConstant.ORDER_PAY_WAY_WEIXIN);              // 先给定一个假数据 1101
        long timestamp = SignHelper.getTimestamp();
        orderInfoVo.setTimestamp(timestamp);  // 获取当前系统时间的毫米值
        String orderInfoVoJson = JSON.toJSONString(orderInfoVo);
        Map<String , Object> map = JSON.parseObject(orderInfoVoJson, Map.class);
        String sign = SignHelper.getSign(map);
        orderInfoVo.setSign(sign);

        // 把交易号保存到Redis中
        redisTemplate.opsForValue().set( RedisConstant.ORDER_TRADE_NO_PREFIX + tradeNo , tradeNo , 5 , TimeUnit.MINUTES);


    }
}
